﻿import DataSourceForm from './DataSourceForm.vue'
import VTitle from '@/components/common/MainTitle.vue'
export default {
  components: {
    DataSourceForm, VTitle
  },
  data () {
    return {
      titleName: '数据源管理',
      loading: false,
      // 弹窗的信息
      dialogInfo: {
        operationType: '',
        dialogFormVisible: false,
        rowInfo: null
      },
      // 所有的数据
      allData: [],
      // 右侧的表格
      tableData: [],
      tableColumn: [
        {
          text: '数据源名称',
          indexName: 'SourceName'
        },
        {
          text: '数据源类型',
          indexName: 'SourceCategory_Text'
        },
        {
          text: '连接字符串',
          indexName: 'ConnectStr'
        },
        {
          text: '是否启用',
          indexName: 'IsActive'
        }
      ],
      // 页码部分
      totalLength: 0,
      // 获取表格数据的查询参数
      requestParams: {
        limit: 10,
        page: 1,
        SourceName: '',
        SourceCategory: ''
      },
      sourceCategoryList: [] // 数据源类型
    }
  },
  created () {
    this.getAllData()
    this.getAllSourceCategoryInfo()
  },
  methods: {
    // 获取所有数据源类型
    getAllSourceCategoryInfo () {
      this.$get('/data_source/category').then((data) => {
        let dataResult = data.data.result
        if (dataResult.code === 'success') {
          this.sourceCategoryList = dataResult.data
        } else {
          this.$message({
            message: dataResult.msg,
            type: 'warning'
          })
        }
      }).catch((error) => {
        this.$message({
          message: error,
          type: 'error'
        })
      })
    },
    // 获取所有数据源列表
    getAllData () {
      this.loading = true
      this.$get('/icpdatasource').then((data) => {
        if (data.status === 200) {
          this.allData = data.data.result
          this.totalLength = this.allData.length
          // 调用方法进行页面渲染
          this.showData()
        } else {
          this.$message({
            message: data.statusText,
            type: 'warning'
          })
        }
        this.loading = false
      }).catch((error) => {
        this.$message({
          message: error,
          type: 'error'
        })
      })
    },
    // 将页码与数据关联
    showData () {
      // 渲染表格的数据存放在 this.tableData 中
      this.tableData = []
      let resultData = this.allData.length ? this.allData : []
      if (this.requestParams.SourceName || this.requestParams.SourceCategory) {
        resultData = []
        this.allData.forEach(item => {
          let surePush = false
          if (this.requestParams.SourceName && this.$trim(this.requestParams.SourceName) && this.requestParams.SourceCategory) {
            // 两个查询条件都存在时,必须同时满足
            if (item.SourceCategory === this.requestParams.SourceCategory && item.SourceName.indexOf(this.requestParams.SourceName) !== -1) {
              surePush = true
            }
          } else {
            // 只有一个查询条件时,满足一个即可
            if (item.SourceCategory === this.requestParams.SourceCategory) {
              surePush = true
            }
            if (this.requestParams.SourceName && this.$trim(this.requestParams.SourceName)) {
              surePush = item.SourceName.indexOf(this.requestParams.SourceName) !== -1
            }
          }
          if (surePush) {
            resultData.push(item)
          }
        })
      }
      if (resultData.length) {
        let from = (this.requestParams.page - 1) * this.requestParams.limit
        let to = this.requestParams.page * this.requestParams.limit
        if (to > resultData.length) {
          to = resultData.length
        }
        for (; from < to; from++) {
          this.tableData.push(resultData[from])
        }
      }
    },
    // 查询时
    getSearchInfo () {
      this.requestParams.page = 1
      this.showData()
    },
    // 底部页码的部分
    handleSizeChange (val) {
      this.requestParams.limit = val
      this.showData()
    },
    handleCurrentChange (val) {
      this.requestParams.page = val
      this.showData()
    },
    // 新增时
    handleAdd () {
      this.dialogInfo.rowInfo = null
      this.dialogInfo.operationType = 'add'
      this.dialogInfo.dialogFormVisible = true
    },
    // 编辑时
    handleEdit (row) {
      this.dialogInfo.rowInfo = JSON.parse(JSON.stringify(row))
      this.dialogInfo.operationType = 'edit'
      this.dialogInfo.dialogFormVisible = true
    },
    // 查看详细时
    handleDetail (row) {
      this.dialogInfo.rowInfo = JSON.parse(JSON.stringify(row))
      this.dialogInfo.operationType = 'preview'
      this.dialogInfo.dialogFormVisible = true
    },
    // 编辑或者新增成功时
    operationSuccess () {
      this.dialogInfo.rowInfo = null
      this.dialogInfo.operationType = ''
      this.dialogInfo.dialogFormVisible = false
      this.getAllData()
    },
    // 删除
    handleDelete (row) {
      this.$confirm('此操作将永久删除该数据源信息, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        cancelButtonClass: 'el-button--info'
      }).then(() => {
        this.loading = true
        this.submitDelete(row)
      }).catch(() => {})
    },
    // 确定删除时
    submitDelete (row) {
      this.$delete('/icpdatasource/' + row.SourceUID).then(data => {
        if (data.status === 204) {
          this.$message({
            message: '删除成功',
            type: 'success'
          })
          this.getAllData()
        } else {
          this.$message({
            message: data.statusText,
            type: 'warning'
          })
          this.loading = false
        }
      }).catch(error => {
        console.log(error)
      })
    },
    // 启用的方法
    handleActive (row) {
      this.loading = true
      this.$patch('/icpdatasource/' + row.SourceUID + '/active').then(data => {
        let dataResult = data.data.result
        if (dataResult.code === 'success') {
          this.$message({
            message: '启用成功',
            type: 'success'
          })
          this.getAllData()
        } else {
          this.$message({
            message: dataResult.msg,
            type: 'warning'
          })
          this.loading = false
        }
      }).catch(error => {
        console.log(error)
      })
    },
    // 停用的方法
    handleDeactive (row) {
      this.loading = true
      this.$patch('/icpdatasource/' + row.SourceUID + '/deactive').then(data => {
        let dataResult = data.data.result
        if (dataResult.code === 'success') {
          this.$message({
            message: '停用成功',
            type: 'success'
          })
          this.getAllData()
        } else {
          this.$message({
            message: dataResult.msg,
            type: 'warning'
          })
          this.loading = false
        }
      }).catch(error => {
        console.log(error)
      })
    }
  }
}
